#!/usr/bin/perl -w
use strict;
use v5.10;	#for say feature.
use utf8;

use Data::Dumper;
use Net::LDAP;
use Config::Simple;

my $cfg = new Config::Simple($ENV{'HOME'} . '/.ldapconf');
#$cfg->param('binddn');
my $ldap = Net::LDAP->new($cfg->param('host'));
$ldap->bind($cfg->param('binddn'), password => $cfg->param('password'));
#实践中使用callback对大量数据进行处理时，会有很多异常情况。先取出所有数据，再循环处理反而更加稳定。
my $msg = $ldap->search(base=>'ou=info,ou=students,ou=people,dc=jhc,dc=cn',
			scope=>'subtree',
#			sizelimit => 1,
			filter=> '(objectClass=posixAccount)'
		       );
$msg->code && die $msg->error;
foreach my $entry ($msg->entries) {
  say "processing: " . $entry->dn;
  my $modmsg = $ldap->moddn($entry->dn,
			    newrdn => "uid=" . $entry->get_value('uid'),
			    newsuperior => 'ou=info,ou=people,dc=jhc,dc=cn');
  $modmsg->code && die $modmsg->error;
}

$ldap->unbind;

